<?php
	class Nguoidung_model extends CI_Model
	{
		function LayNCC_NDHienHanh()
		{
			$this->db->select('*');
			$this->db->from('NhaCungCap');
			$this->db->where('TinhTrangTaiKhoan = 1');
			$this->db->limit(20, $this->uri->segment(3));
			$this->db->order_by("MaNCC","desc");
			$query = $this->db->get();

			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		
		function TimNCC_NDHienHanh($key)
		{
			$post_per_page = 20;
			$num = 0;
			if($this->uri->segment(3))
				$num = $this->uri->segment(3);
				
			$strSQL = "SELECT *
						FROM NhaCungCap
						WHERE TinhTrangTaiKhoan = 1 and (Ten like '%$key%' or Username like '%$key%')
						ORDER BY MaNCC desc
						LIMIT $num, $post_per_page";
			$query = $this->db->query($strSQL);								
			
			$total_query = "SELECT *
						FROM NhaCungCap
						WHERE TinhTrangTaiKhoan = 1 and (Ten like '%$key%' or Username like '%$key%')";	
			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nguoidung/admin_index';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		
		function TimNV_NDHienHanh($key)
		{
			$post_per_page = 20;
			$num = 0;
			if($this->uri->segment(3))
				$num = $this->uri->segment(3);
				
			$strSQL = "SELECT *
						FROM NhanVien
						WHERE TinhTrangTaiKhoan = 1 and (HoTen like '%$key%' or Username like '%$key%')
						ORDER BY MaNV desc
						LIMIT $num, $post_per_page";
			$query = $this->db->query($strSQL);								
			
			$total_query = "SELECT *
						FROM NhanVien
						WHERE TinhTrangTaiKhoan = 1 and (HoTen like '%$key%' or Username like '%$key%')";	
			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nguoidung/admin_index';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		
		function TimNCC_NDDisable($key)
		{
			$post_per_page = 20;
			$num = 0;
			if($this->uri->segment(3))
				$num = $this->uri->segment(3);
				
			$strSQL = "SELECT *
						FROM NhaCungCap
						WHERE TinhTrangTaiKhoan = 0 and (Ten like '%$key%' or Username like '%$key%')
						ORDER BY MaNCC desc
						LIMIT $num, $post_per_page";
			$query = $this->db->query($strSQL);								
			
			$total_query = "SELECT *
						FROM NhaCungCap
						WHERE TinhTrangTaiKhoan = 0 and (Ten like '%$key%' or Username like '%$key%')";	
			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nguoidung/NDDisable';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		
		function TimNV_NDDisable($key)
		{
			$post_per_page = 20;
			$num = 0;
			if($this->uri->segment(3))
				$num = $this->uri->segment(3);
				
			$strSQL = "SELECT *
						FROM NhanVien
						WHERE TinhTrangTaiKhoan = 0 and (HoTen like '%$key%' or Username like '%$key%')
						ORDER BY MaNV desc
						LIMIT $num, $post_per_page";
			$query = $this->db->query($strSQL);								
			
			$total_query = "SELECT *
						FROM NhanVien
						WHERE TinhTrangTaiKhoan = 0 and (HoTen like '%$key%' or Username like '%$key%')";	
			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nguoidung/NDDisable';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		
		function LayNV_NDHienHanh()
		{
			$this->db->select('*');
			$this->db->from('NhanVien');
			$this->db->where('TinhTrangTaiKhoan = 1');
			$this->db->limit(20, $this->uri->segment(3));
			$this->db->order_by("MaNV","desc");
			$query = $this->db->get();
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		function LayNCC_NDDisable()
		{
			$this->db->select('*');
			$this->db->from('NhaCungCap');
			$this->db->where('TinhTrangTaiKhoan = 0');
			$this->db->limit(20, $this->uri->segment(3));
			$this->db->order_by("MaNCC","desc");
			$query = $this->db->get();

			/*$this->load->library('pagination');
			$config['base_url'] = 'index.php/nguoidung/NDDisable';
			$config['total_rows'] = $this->db->get('NhaCungCap')->num_rows();			
			$config['per_page'] = 20;
			$this->pagination->initialize($config);	*/
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		function LayNV_NDDisable()
		{
			$this->db->select('*');
			$this->db->from('NhanVien');
			$this->db->where('TinhTrangTaiKhoan = 0');
			$this->db->limit(20, $this->uri->segment(3));
			$this->db->order_by("MaNV","desc");
			$query = $this->db->get();

			/*$this->load->library('pagination');
			$config['base_url'] = 'index.php/nguoidung/NDDisable';
			$config['total_rows'] = $this->db->get('nhanvien')->num_rows();			
			$config['per_page'] = 20;
			$this->pagination->initialize($config);	*/
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		
		function DisableTKNCC($mancc)
		{
			$state = 0;
			$this->db->update('NhaCungCap', array('TinhTrangTaiKhoan' => $state), array('MaNCC' => $mancc));
			return $this->db->affected_rows();	
		}
		
		function DisableTKNV($manv)
		{
			$state = 0;
			$this->db->update('NhanVien', array('TinhTrangTaiKhoan' => $state), array('MaNV' => $manv));
			return $this->db->affected_rows();	
		}
		
		function ResetPassNCC($mancc)
		{
			$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
			$pass = array(); //remember to declare $pass as an array
			$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
			for ($i = 0; $i < 8; $i++)
			{
				$n = rand(0, $alphaLength);
				$pass[] = $alphabet[$n];
    		}
			$pass = implode($pass);
			$pass = md5($pass);
			$this->db->update('NhaCungCap', array('Password' => $pass), array('MaNCC' => $mancc));
			return $this->db->affected_rows();	
		}
		
		function ResetPassNV($manv)
		{
			$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
			$pass = array(); //remember to declare $pass as an array
			$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
			for ($i = 0; $i < 8; $i++)
			{
				$n = rand(0, $alphaLength);
				$pass[] = $alphabet[$n];
    		}
			$pass = implode($pass);
			$pass = md5($pass);
			$this->db->update('NhanVien', array('Password' => $pass), array('MaNV' => $manv));
			return $this->db->affected_rows();	
		}
		
		function EnableTKNCC($mancc)
		{
			$state = 1;
			$this->db->update('NhaCungCap', array('TinhTrangTaiKhoan' => $state), array('MaNCC' => $mancc));
			return $this->db->affected_rows();	
		}
		
		function EnableTKNV($manv)
		{
			$state = 1;
			$this->db->update('NhanVien', array('TinhTrangTaiKhoan' => $state), array('MaNV' => $manv));
			return $this->db->affected_rows();	
		}
	}
?>